/*
 * request.h
 *
 *  Created on: 2021年8月16日
 *      Author: pengzc
 */

#ifndef HTTP_HTTP_REQUEST_H_
#define HTTP_HTTP_REQUEST_H_

#include <string>
#include <map>
#include "mime.h"

namespace http {

class HTTPClient;

class HTTPRequest {
public:
  /**
   * GET / POST
   */
  HTTPRequest(const std::string& method, const std::string& url);
  HTTPRequest(const std::string& method, const std::string& url,
      const std::string& body);

  HTTPRequest(const std::string& method, const std::string& url,
      const Mime& body);

  virtual ~HTTPRequest();

public:
  typedef struct {
    double totalTime;
    double nameLookupTime;
    double connectTime;
    double appConnectTime;
    double preTransferTime;
    double startTransferTime;
    double redirectTime;
    int redirectCount;
  } RequestInfo;

  typedef struct {
    std::string baseUrl;
    int timeout;
    bool followRedirects;
    int maxRedirects;
    bool noSignal;
    struct {
      std::string username;
      std::string password;
    } basicAuth;

    std::string certPath;
    std::string certType;
    std::string keyPath;
    std::string keyPassword;
    std::string customUserAgent;
    std::string uriProxy;
    RequestInfo lastRequest;
  } Info;

  // append additional headers
  HTTPRequest& appendHeader(const std::string& key, const std::string& value);

  //Header Content-Type
  HTTPRequest& setContentType(const std::string& content_type);

  /**
   * @brief 断点恢复，需要服务器支持
   * @param offset
   */
  void setResumeFrom(int offset);

private:
  friend class HTTPClient;

  std::string method_;
  std::string url_;
  std::string body_;
  const Mime* mime_ = NULL;

  int resume_from_ = 0;

  typedef std::map<std::string, std::string> HeaderFields;
  HeaderFields header;
  bool follow_redirects_;
  int max_redirects;
};

} /* namespace http */

#endif /* HTTP_HTTP_REQUEST_H_ */
